﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

using AutoFacUtils;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.Web.Areas.ShopManage.Models.User.Result;
using CashFlow.Web.Def;

namespace CashFlow.Web.Areas.ShopManage.Service.Implement
{
    public class UserService : MyService<UserInfoEntity>, IUserService
    {
        public IList<UserInfoModel> QueryShopAllUsers(int shopManagerUserId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<UserInfoEntity>()
                           .InnerJoin<ShopUserEntity>((u, su) => u.Id == su.UserId)
                           .Where((u, su) => su.ShopManagerUserId == shopManagerUserId)
                           .Select((u, su) => new UserInfoModel
                           {
                               UserId = u.Id,
                               Name = u.Name,
                               Phone = u.Phone
                           })
                           .ToList();
                return ls;
            }
        }

        public IList<UserInfoModel2> QueryByWhere(
            int shopManagerUserId
            , string phone
            , string name
            , string invitationCode
            , string reference
            , string referenceInvitationCode
            , int? state)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var query = db.Queryable<UserInfoEntity>()
                              .InnerJoin<UserInfoEntity>((u, u2) => u.ReferenceInvitationCode == u2.InvitationCode)
                              .InnerJoin<ShopUserEntity>((u, u2, su) => u.Id == su.UserId)
                              .Where((u, u2, su) => su.ShopManagerUserId == shopManagerUserId);

                if (!string.IsNullOrEmpty(phone))
                {
                    query = query.Where((u, u2, su) => u.Phone.Contains(phone));
                }
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where((u, u2, su) => u.Name.Contains(name));
                }
                if (!string.IsNullOrEmpty(invitationCode))
                {
                    query = query.Where((u, u2, su) => u.InvitationCode.Contains(invitationCode));
                }
                if (!string.IsNullOrEmpty(reference))
                {
                    query = query.Where((u, u2, su) => u2.Name.Contains(reference));
                }
                if (!string.IsNullOrEmpty(referenceInvitationCode))
                {
                    query = query.Where((u, u2, su) => u.ReferenceInvitationCode.Contains(referenceInvitationCode));
                }
                if (state.HasValue)
                {
                    query = query.Where((u, u2, su) => u.State == state.Value);
                }

                var ls = query.OrderByDescending((u, u2, su) => u.CreateTime)
                              .Select((u, u2, su) => new UserInfoModel2
                              {
                                  Id = u.Id,
                                  Name = u.Name,
                                  InvitationCode = u.InvitationCode,
                                  CreateTime = u.CreateTime,
                                  Phone = u.Phone,
                                  Reference = u2.Name,
                                  ReferenceInvitationCode = u.ReferenceInvitationCode,
                                  State = u.State,
                                  ShopManagerLevelPercent = u.ShopManagerLevelPercent
                              })
                              .ToList();
                return ls;
            }
        }

        public bool IsReferenceInvitationCodeInShop(
            int shopManagerUserId
            , int userId
            , string referenceInvitationCode)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int count = db.Queryable<UserInfoEntity>()
                              .InnerJoin<ShopUserEntity>((u, su) => u.Id == su.UserId)
                              .Where((u, su) => su.ShopManagerUserId == shopManagerUserId
                                    && u.InvitationCode == referenceInvitationCode
                                    && u.Id != userId)
                              .Count();
                return count > 0;
            }
        }

        public bool ResetPwd(int userId, string pwd)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = new UserInfoEntity();
                entity.Id = userId;
                db.Tracking(entity);
                entity.Password = pwd;
                int n = db.Updateable(entity).ExecuteCommand();
                return n > 0;
            }
        }

        public bool ResetState(int userId, int state)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = new UserInfoEntity();
                entity.Id = userId;
                entity.State = state;
                int n = db.Updateable(entity)
                          .UpdateColumns(x => x.State)
                          .WhereColumns(x => x.Id)
                          .ExecuteCommand();
                return n > 0;
            }
        }

        public bool ResetShopManagerLevelPercent(
            int userId
            , decimal percent)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Updateable<UserInfoEntity>()
                          .SetColumns(x => new UserInfoEntity
                          {
                              ShopManagerLevelPercent = percent
                          })
                          .Where(x => x.Id == userId)
                          .ExecuteCommand();
                return n > 0;
            }
        }

        public bool UpdateUserReference(
            int userId
            , string referenceInvitationCode)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Updateable<UserInfoEntity>()
                          .SetColumns(x => new UserInfoEntity
                          {
                              ReferenceInvitationCode = referenceInvitationCode
                          })
                          .Where(x => x.Id == userId)
                          .ExecuteCommand();
                return n > 0;
            }
        }

        public EUpdateUserInfoResult UpdateUserInfo(
            int userId
            , string name
            , string phone)
        {
            var result = EUpdateUserInfoResult.Other;

            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Queryable<UserInfoEntity>()
                          .Where(x => x.Id != userId
                            && x.Phone == phone)
                          .Count();
                if (n > 0)
                {
                    result = EUpdateUserInfoResult.RepeatPhone;
                }
                else
                {
                    int n2 = db.Updateable<UserInfoEntity>()
                               .SetColumns(x => new UserInfoEntity
                               {
                                   Name = name,
                                   Phone = phone
                               })
                               .Where(x => x.Id == userId)
                               .ExecuteCommand();
                    if (n2 >= 0)
                    {
                        result = EUpdateUserInfoResult.Success;
                    }
                }
            }

            return result;
        }
    }
}
